package com.check.checksamejourney.controller;


import com.check.checksamejourney.bean.Subscribe;
import com.check.checksamejourney.mapper.SubscribeMapper;
import com.check.checksamejourney.mapper.UserMapper;
import com.check.checksamejourney.response.BaseResponse;
import com.check.checksamejourney.response.ResponseState;
import com.check.checksamejourney.response.SubscribeListResult;
import com.check.checksamejourney.response.SubscribeSetResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/subscribe")
public class SubscribeController {

    @Autowired
    SubscribeMapper subscribeMapper;

    @Autowired
    UserMapper userMapper;

    @GetMapping("/set")
    public BaseResponse<SubscribeSetResult> set(String token, Subscribe subscribe){
        BaseResponse response = new BaseResponse();
        SubscribeSetResult result = new SubscribeSetResult();
        response.setResult(result);

        if (subscribe.getId()!=null){
            // ID不为空即修改

            if (subscribeMapper.updateSubscribe(subscribe)==0){
                // 更新失败
                response.setResponseState(ResponseState.DATABASE_OPERATION_FAILED);
            }else {
                response.setResponseState(ResponseState.SUCCESS);
                result.setSubscribe(subscribe);
            }
        }else {
            // ID为空即新增

            // 获取userID
            Long userId = userMapper.getUserIdByToken(token);
            if (userId == null){
                // token无效
                response.setResponseState(ResponseState.TOKEN_INVALID);
            }else {
                subscribe.setUserId(userId);
                subscribe.setState(Subscribe.SUBSCRIBING);
                if (subscribeMapper.insertSubscribe(subscribe)==0){
                    // 插入失败
                    response.setResponseState(ResponseState.DATABASE_OPERATION_FAILED);
                }else {
                    // 新建
                    subscribe.setUserId(null);
                    response.setResponseState(ResponseState.SUCCESS);
                    result.setSubscribe(subscribe);
                }
            }
        }

        return response;
    }

    @GetMapping("/list")
    public BaseResponse<SubscribeListResult> list(String token){
        BaseResponse response = new BaseResponse();
        SubscribeListResult result = new SubscribeListResult();
        response.setResult(result);

        Long userId = userMapper.getUserIdByToken(token);
        if (userId==null){
            // token无效
            response.setResponseState(ResponseState.TOKEN_INVALID);
        }else {
            response.setResponseState(ResponseState.SUCCESS);
            result.setSubscribes(subscribeMapper.getSubscribesByUserId(userId));
        }

        return response;
    }

}
